package co.za.itweb.securedata;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("SetJavaScriptEnabled")
public class MyWeb extends FragmentActivity{
	
	private static final int ID_MYNEWS = 1;
	private static final int ID_SECTORNEWS = 2;
	private static final int ID_FINDUS = 3;
	private static final int ID_ABOUTUS = 4;
	private static final int ID_MYWEB = 5;
	 QuickAction mQuickAction;
	String title;
	TextView text;
	private int feedValue;
	private WebView myWebView;
	
	private int width;
	private int height;
	private ProgressDialog progDialog;


	
	public void onCreate(Bundle icecream) {
		
		super.onCreate(icecream);
		setContentView(R.layout.myweb);

		
		
			title = First.PAGE5TITLE;
		
		
		RelativeLayout newsbackgroundlayout = (RelativeLayout) findViewById(R.id.newsbackground);
		newsbackgroundlayout.setBackgroundColor(Color.parseColor(First.BACKGROUND));
	
		
		
		LinearLayout layout = (LinearLayout) findViewById(R.id.cell3);
		
	    int colors[] =  {Color.parseColor(First.STARTCOLOR),Color.parseColor(First.ENDCOLOR)};
		GradientDrawable grad = new GradientDrawable(Orientation.TOP_BOTTOM,colors);
		layout.setBackgroundDrawable(grad);
		

		

		ActionItem mynews = new ActionItem(ID_MYNEWS,  First.PAGE1TITLE,getDrawableFromSD(First.mynewsname, getResources()
				.getDrawable(R.drawable.mynews) ));
		

		

		ActionItem sectornews = new ActionItem(ID_SECTORNEWS,  First.PAGE2TITLE,
				getDrawableFromSD(First.sectornewsname, getResources()
						.getDrawable(R.drawable.sectornews) ));
		ActionItem findus = new ActionItem(ID_FINDUS,  First.PAGE3TITLE, getDrawableFromSD(First.findusname, getResources()
				.getDrawable(R.drawable.findus) ));
		ActionItem aboutus = new ActionItem(ID_ABOUTUS,  First.PAGE4TITLE,
				getDrawableFromSD(First.aboutusname, getResources()
						.getDrawable(R.drawable.findus) ));


		mQuickAction = new QuickAction(getApplicationContext(),QuickAction.VERTICAL);

		
		
		mQuickAction.addActionItem(mynews);
		mQuickAction.addActionItem(sectornews);
		mQuickAction.addActionItem(findus);
		mQuickAction.addActionItem(aboutus);

		
		mQuickAction
		.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
			@Override
			public void onItemClick(QuickAction quickAction, int pos,
					int actionId) {
			

				if (actionId == ID_SECTORNEWS) {
					Intent intent = new Intent(getApplicationContext(),
							Second.class);

					intent.putExtra("feedid", feedValue);

					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);

				}else if (actionId == ID_MYNEWS) {
					Intent intent = new Intent(getApplicationContext(),
							First.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);

				} 
				
				else if (actionId == ID_FINDUS) {
					Intent intent = new Intent(getApplicationContext(),
							DialogActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);

				} else if (actionId == ID_MYWEB) {
					Intent intent = new Intent(getApplicationContext(), MyWeb.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);

				}

				else if (actionId == ID_ABOUTUS) {
					Intent intent = new Intent(getApplicationContext(), About.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				} else {

				}
			}
		});
		
		mQuickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
			@Override
			public void onDismiss() {

			}
		});
		

		TextView titletext = (TextView) findViewById(R.id.title);
		titletext.setText(title);

		Button back = (Button) findViewById(R.id.back);
		back.setBackgroundDrawable(getDrawableFromSD(First.backname, getResources()
				.getDrawable(R.drawable.ic_back) ));
		 

		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		});

		Button menu=(Button) findViewById(R.id.menu);
        menu.setBackgroundDrawable(getDrawableFromSD(First.menuname, getResources()
				.getDrawable(R.drawable.menu) ));
		 menu.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					
					
					mQuickAction.show(v);
					mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
					
				}
			});
		
		
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		
		height = metrics.heightPixels;
		 width = metrics.widthPixels;
			RelativeLayout linLayout = (RelativeLayout) findViewById(R.id.mywebholder);
		
			RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(width, RelativeLayout.LayoutParams.WRAP_CONTENT);
			layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
			myWebView = new WebView(MyWeb.this); 
			linLayout.addView(myWebView, layoutParams);

		
		
		

		  myWebView.getSettings().setJavaScriptEnabled(true);
		  myWebView.getSettings().setSupportZoom(true);
		  myWebView.getSettings().setBuiltInZoomControls(true);
		  myWebView.setWebChromeClient(new WebChromeClient() {
      	  
      	 });
		  
		    progDialog = new ProgressDialog(this);
	        progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	        progDialog.setMessage("Loading page...");
	        progDialog.setCancelable(true);
	        progDialog.setMax(100);
	        progDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
		          public void onCancel(DialogInterface dialog) {
		             
		    			finish();
		          }
		    });
	        progDialog.show();

		  myWebView.setWebViewClient(new WebViewClient(){
			  @Override
			  public boolean shouldOverrideUrlLoading(WebView view, String url) {
				  if (url != null && url.startsWith("http://")) {
			            view.getContext().startActivity(
			                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
			            return true;
			        } else {
			            return false;
			        }
			  }
			  
			  @Override  
		        public void onPageFinished(WebView view, String url) {  
				  if(progDialog.isShowing()){
					  progDialog.dismiss();
				  }
			  }
		  
		  });
		  
		  
		  if(First.PAGE5URL.length()<3){ 
			  myWebView.loadData(First.PAGE5TEXT,"text/html",null);  	  
		  }else{
			  myWebView.loadUrl(First.PAGE5URL); 
		  }
	        
        
        
		
		
		
		

	}

	



	public Drawable getDrawableFromSD(String imagename, Drawable defaultdrawable){
		File path = new File( Environment.getExternalStorageDirectory()+"/"+First.FOLDERNAME+"/"+imagename);
		 if(path.exists() ) {
		try{
		Bitmap bitmapImage = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/"+First.FOLDERNAME+"/"+imagename);
		Drawable bgrImage;
        bgrImage = new BitmapDrawable(getResources(),bitmapImage);
		   
		   return bgrImage;
		   
		}catch(Exception ex){
			System.out.println(ex.toString());
			return defaultdrawable;
		}
		 }else{
			 return defaultdrawable;
		 }
	   }

	
	
	@Override
	protected void onPause() {
		
		if(mQuickAction != null && mQuickAction.mWindow.isShowing()){
			mQuickAction.dismiss();
		
		}
		overridePendingTransition(R.anim.hold,R.anim.push_out_to_left);
		super.onPause();
	}
	

}
